<?php
class jpush {
	/**
	 *
	 * @param  $masterSecret 极光推送portal 上分配的 appKey 的验证串(masterSecret)  masterSecret = "a28ccfc701e24d97e6391fe8";
	 * @param  $appKey  极光推送portal 上分配的 appKey 的验证串(appKey ) appKey = "776e0be5df808936c82323ae";
	 * @param  $msgType 发送消息的类型：１、通知 ２、自定义消息（只有 Android 支持） 1
	 * @param  $sendNo 发送编号 建议每次推送这个编号递增。  随机数
	 * @param  $msgTitle 通知标题。
	 * @param  $msgContent 通知内容
	 * @param  $extra 通知附加参数。JSON格式。客户端可取得全部内容。
	 * @param  $builderId 通知样式 默认为 0
	 * @param  $receiverType 接收者类型 1、指定的 IMEI 2、指定的 tag 3、指定的 alias4、 对指定 app_key 的所有用户推送消息
	 * @param  $receiverValue 发送范围值，与 receiver_type 相对应。 1、IMEI只支持一个2、tag 支持多达 10 个，使用 "," 间隔3、alias 支持多达 10 个，使用 "," 间隔。4、不需要填
	 * @return mixed
	 * @description JPush 推送消息
	 * @version 1.0
	 * @author xieruibing
	 * @update 2013-4-1 下午2:50:20
	 */
	public function sendNotification($masterSecret, $appKey,$sendNo, $msgTitle, $msgContent,$extra,$builderId,$receiverType,$receiverValue) {
		//print_r("1232123123");
		if($builderId==""||$builderId==null)
			$builderId = 0;
		if($extra==""||$extra==null)
			$extra = "{}";
		
		//$msgTitle = $this->iconvToUtf ( $msgTitle );
		//$msgContent = $this->iconvToUtf ( $msgContent );
		$extra = $this->iconvToUtf ( $extra );
		$msgContents = "{\"n_builder_id\":\"$builderId\",\"n_title\":\"$msgTitle\",\"n_content\":\"$msgContent\",\"n_extras\":$extra}";
		//$msgContents = $this->iconvToUtf ( $msgContents );
		//echo $msgContents;
		$msgContents =urlencode($msgContents);
	
		if($receiverType=='imei')
			$receiverType=1;
		else if($receiverType=='tag')
			$receiverType=2;
		else if($receiverType=='alias')
			$receiverType=3;
		else if($receiverType=='app')
			$receiverType=4;
	
		$verificationCode = md5 ( $sendNo . $receiverType. $receiverValue . $masterSecret );
	
		//if($isSSL=='1')
		$baseUrl = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg";
			// 		else if($isSSL=='2')
				// 			$baseUrl = "https://api.jpush.cn:443/sendmsg/v2/sendmsg";
		$params = "msg_content=$msgContents&platform=android&sendno=$sendNo&receiver_type=$receiverType&receiver_value=$receiverValue&app_key=$appKey&verification_code=$verificationCode&msg_type=1";
		//$url = $baseUrl . '?' . $params;
		$data = $this->getJPushData($baseUrl,$params);
		
		return $data;
	}
	
	public function sendMessage($masterSecret, $appKey,$sendNo, $msgTitle, $message,$extra,$receiverType,$receiverValue) {
		if($extra==""||$extra==null)
			$extra = "{}";
		$msgContents = "{\"title\":\"$msgTitle\",\"message\":\"$message\",\"extras\":$extra}";
		//$msgContents = $this->iconvToUtf ( $msgContents );
		$msgContents =urlencode($msgContents);
	
		if($receiverType=='imei')
			$receiverType=1;
		else if($receiverType=='tag')
			$receiverType=2;
		else if($receiverType=='alias')
			$receiverType=3;
		else if($receiverType=='app')
			$receiverType=4;
	
		$verificationCode = md5 ( $sendNo . $receiverType. $receiverValue . $masterSecret );
	
		//if($isSSL=='1')
		$baseUrl = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg";
			// 		else if($isSSL=='2')
				// 			$baseUrl = "https://api.jpush.cn:443/sendmsg/v2/sendmsg";
		$params = "msg_content=$msgContents&platform=android&sendno=$sendNo&receiver_type=$receiverType&receiver_value=$receiverValue&app_key=$appKey&verification_code=$verificationCode&msg_type=2";
		//$url = $baseUrl . '?' . $params;
		$data = $this->getJPushData($baseUrl,$params);
	
		return $data;
	}
	
	public function iconvToUtf($value) {
		$encode = mb_detect_encoding($value, array("ASCII","CP936","GB2312","GBK","UTF-8"));
		if ($encode != "utf-8") {
			$value = iconv ( $encode, "utf-8", $value );
		}
		return $value;
	}
	
	public function getJPushData($url,$params) {
		$curl = curl_init (); // 初始化一个 cURL 对象
		curl_setopt ( $curl, CURLOPT_URL, $url ); // 设置你需要抓取的URL
		//curl_setopt ( $curl, CURLOPT_HEADER, 1 ); // 设置header
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 设置cURL
		curl_setopt($curl, CURLOPT_POST, 1);//设置为POST方式
		curl_setopt($curl,  CURLOPT_POSTFIELDS, $params);//设置为POST方式
		$data = curl_exec ( $curl ); // 运行cURL，请求网页
		curl_close ( $curl ); // 关闭URL请求
		return $data;
	}
}